let { api, imgUrl } = require("./../../utils/api.js");
let { getTime, randomVal } = require("./../../utils/constant.js");
let placeArr = [
  "哇喔~，这个菜真是 . . .",
  "我的天，这个菜让我 . . .",
  "请您对本菜评价",
  "嗝~~,不错不错，整个 . . ."
];
Page({
  data: {
    evaluates: ["差", "一般", "还不错", "很满意", "强烈推荐"], //总体评价
    checkIndex: 2, // 当前选中的整体评价的下标
    labels: [
      [
        { labelId: 1, text: "卫生不好" },
        { labelId: 2, text: "分量少" },
        { labelId: 3, text: "太辣" },
        { labelId: 4, text: "太咸" },
        { labelId: 5, text: "太甜" },
        { labelId: 6, text: "太酸" },
        { labelId: 7, text: "太苦" },
        { labelId: 8, text: "无味" }
      ],
      [
        { labelId: 9, text: "干净卫生" },
        { labelId: 10, text: "卖相不错" },
        { labelId: 11, text: "味道一般" },
        { labelId: 12, text: "分量足" },
        { labelId: 13, text: "味道赞" },
        { labelId: 14, text: "有营养" }
      ]
    ],
    checkLabel: [], // 选中的评价标签ID
    contentText: "", //输入框内容
    placeholder: randomVal(placeArr), ////输入框的提示文案
    wordCount: 0, //剩余字数
    isAnonymous: false
  },
  onLoad: function(options) {
    console.log(options);
    let { dishName, dishId } = options;
    this.setData({
      dishName,
      dishId
    });
  },

  // 切换整体评价
  selectEvaluates: function(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    if (
      (id > 1 && this.data.checkIndex <= 1) ||
      (id <= 1 && this.data.checkIndex > 1)
    ) {
      // 切换整体评价时，清空之前选中的标签
      let param = {};
      that.data.checkLabel.forEach(function(item, index, arr) {
        let isCheck = "isCheck" + item;
        param[isCheck] = !that.data[isCheck];
        that.setData(param);
      });
      // 清空所有选中的标签
      this.setData({
        checkLabel: []
      });
    }
    this.setData({
      checkIndex: id
    });
    console.log(this.data.checkLabel);
  },

  // 选择评价标签
  selectLabel: function(e) {
    let that = this;
    let labelId = Number(e.currentTarget.dataset.labelid);
    let isCheck = "isCheck" + labelId;
    // 以下通过对象的形式可以动态的改变setData方法中对象的key值
    let param = {};
    let arr = that.data.checkLabel;
    param[isCheck] = !this.data[isCheck];
    //控制最多选择4个标签
    if ((arr.length < 4 && !that.data[isCheck]) || that.data[isCheck]) {
      this.setData(param, function() {
        if (that.data[isCheck]) {
          // 往选中的标签数组中添加标签
          if (arr.indexOf(labelId) < 1) {
            arr.push(Number(labelId));
            that.setData({
              checkLabel: arr
            });
          }
        } else {
          // 在选中的标签数组中删除对应的标签
          let index = arr.indexOf(labelId);
          if (index > -1) {
            arr.splice(index, 1);
            that.setData({
              checkLabel: arr
            });
          }
        }
      });
    } else {
      wx.showToast({
        title: "亲，只能选4个",
        image: "./../../images/public/tips.png",
        duration: 2000
      });
    }
  },

  //统计评价剩余字数
  changContent: function(e) {
    this.setData({
      evaluationContent: e.detail.value.trim(),
      wordCount: e.detail.value.length
    });
  },

  // 是否匿名
  gotoAnonymous: function(e) {
    this.setData({
      isAnonymous: !this.data.isAnonymous
    });
  },

  // 校验输入数据
  filterData: function() {
    let { checkLabel, evaluationContent } = this.data;
    let text = "";
    if (checkLabel.length === 0) {
      text = "请选择相应的标签 ！";
    } else if (!evaluationContent) {
      text = "请输入您的评价内容 ！";
    }
    if (text) {
      wx.showToast({
        title: text,
        icon: "none",
        duration: 2000
      });
      return false;
    } else {
      return true;
    }
  },

  // 发布菜单评价
  gotoRelease: function() {
    let {
      checkLabel,
      evaluationContent,
      checkIndex,
      isAnonymous,
      dishId
    } = this.data;
    if (this.filterData()) {
      let obj = {
        userId: wx.getStorageSync("userInfo").userId, //评价的用户的id*
        evaluationScore: checkIndex + 1, //评分*
        evaluationDate: getTime(1), //评论日期
        evaluationContent: encodeURIComponent(evaluationContent), //评论内容
        dishId, //评论的菜的id*
        taglist: checkLabel, //评论的标签
        anonymous: Number(isAnonymous) //是否匿名，1为匿名，0为不匿名，默认为0
      };
      console.log(obj);
      wx.request({
        url: api + "addDishEvaluation.do",
        method: "POST",
        data: obj,
        success: function(res) {
          console.log(res.data);
          if (res.data.status === 1) {
            wx.showToast({
              title: "发布成功",
              icon: "success",
              duration: 2000
            });
            wx.navigateTo({
              url: "../completeEvaluation/completeEvaluation"
            });
          } else {
            wx.showToast({
              title: "发布失败，请稍后重试!",
              icon: "none",
              duration: 2000
            });
          }
        },
        fail: function(res) {
          wx.showToast({
            title: "网络错误，请稍后重试",
            icon: "none",
            duration: 2000
          });
        }
      });
    }
  }
});
